package com.ruoyi.wechat.api;

import com.google.zxing.WriterException;

import com.ruoyi.common.annotation.Anonymous;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;

import com.ruoyi.wechat.domain.Goods;
import com.ruoyi.wechat.domain.Order;
import com.ruoyi.wechat.service.IGoodsService;
import com.ruoyi.wechat.service.IOrderService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/wechat/goods")
public class WxGoodsController extends BaseController {

    @Autowired
    private IGoodsService goodsService;
    @Autowired
    private IOrderService orderService;


    @GetMapping("/list")
    public AjaxResult list(Goods goods){
        List<Goods> goodsList = goodsService.selectGoodsList(goods);
        return success(goodsList);
    }

    @PostMapping("/add")
    public AjaxResult add(@RequestBody Goods goods)
    {
        return toAjax(goodsService.insertGoods(goods));
    }

    @PostMapping("/save")
    public AjaxResult save(@RequestBody Order order) throws IOException, WriterException {
        String orderId = orderService.insertOrderCopy(order);
        return success(orderId);
    }

    @GetMapping("/saoyisao/{orderId}/{userId}")
    public AjaxResult saoyisao(@PathVariable("orderId") String orderId,@PathVariable("userId") String userId) {
        logger.info("saoyisao", orderId,userId);
        return  toAjax(orderService.saoyisao(orderId,userId));
    }

    @GetMapping("/getList/{userId}")
    public AjaxResult getList(@PathVariable("userId") String userId) {
        List<Order> list = orderService.getByUserIdList(userId);
        return success(list);
    }

    @DeleteMapping("/delOrder/{orderId}")
    public AjaxResult delOrder(@PathVariable("orderId") String orderId) {
        return toAjax(orderService.deOrderSatus(orderId));
    }

    @GetMapping("/getQR/{orderId}")
    public AjaxResult QR(@PathVariable("orderId") String orderId) {
        Order order = orderService.selectOrderByOrderId(orderId);
        String qrcode = order.getQrcode();
        return success(qrcode);
    }

    @DeleteMapping("/delete/{goodsIds}")
    public AjaxResult remove(@PathVariable String[] goodsIds)
    {
        return toAjax(goodsService.deleteGoodsByGoodsIds(goodsIds));
    }

    @GetMapping("/generate")
    @Anonymous
    public AjaxResult generate(@RequestParam String content) throws IOException, WriterException {
        Map<String, Object> result = goodsService.generate(content);

        // 返回包含Base64字符串的AjaxResult
        return success(result);
    }



}
